/************************
* 描述：购物车
* 作者：Gibil
* 时间：2014-1-24
************************/

//重写toFixed，修复Bug
Number.prototype.toFixed = function (exponent) { return parseInt(this * Math.pow(10, exponent) + 0.5) / Math.pow(10, exponent); }

var cart = {
    jsons: [], //数据Json
    comid: '', //商家ID
    //显示购物车商品
    showCart: function () {
        if (!cart.jsons || cart.jsons.length == 0) {
            cart.showNoRecord();
        } else {
            var h = '';
            for (var i = 0; i < cart.jsons.length; i++) {
                for (var j = 0; j < cart.jsons[i].items.length; j++) {
                    h += '<li id="li_' + cart.jsons[i].items[j].id + '">';
                    h += '  <div class="le">';
                    h += '      <div><a><img src="' + cart.jsons[i].items[j].pic + '" /></a></div>';
                    h += '      <div class="le_price"><span>￥' + cart.jsons[i].items[j].price + '</span></div>';
                    h += '  </div>';
                    h += '  <div class="ri">';
                    h += '      <div class="title">' + cart.jsons[i].items[j].proname + '</div>';
                    if (cart.jsons[i].items[j].skutext)
                        h += '  <div class="format">' + cart.jsons[i].items[j].skutext + '</div>';
                    h += '      <div class="ri_num">';
                    h += '          <div class="num">';
                    h += '              <span class="minus" onclick="cart.minusNum(this);"></span>';
                    h += '              <input type="text" value="' + cart.jsons[i].items[j].num + '" maxnum="' + cart.jsons[i].items[j].stocks + '" readonly="readonly" maxlength="3" format="3N" itemid="' + cart.jsons[i].items[j].id + '" price="' + cart.jsons[i].items[j].price + '" comid="' + cart.jsons[i].comid + '" tag="buycount" />';
                    h += '              <span class="plus" onclick="cart.addNum(this);"></span>';
                    h += '          </div>';
                    h += '          <a onclick="cart.delItem(\'' + cart.jsons[i].items[j].id + '\',\'' + cart.jsons[i].comid + '\');" class="rubbish"></a>';
                    h += '      </div>';
                    h += '  </div>';
                    h += '  <div class="clear"></div>';
                    h += '</li>';
                }
            }
            $('#itemContainer ul').append(h);
        }
    },
    //显示没有数据提示
    showNoRecord: function () {
        $('#cart_empty').show();
        $('#itemContainer').hide();
        $('footer').hide();
    },
    //减去
    minusNum: function (obj) {
        var $n = $(obj).parent().find('input');
        var v = parseInt($n.val());
        v = !isNaN(v) ? v - 1 : 1;
        v = v > 0 ? v : 1;
        $.ajax({
            type: 'POST',
            url: '/ajax/cart.ashx',
            dataType: 'text',
            data: { "act": "modify", "qty": v, "itemid": $n.attr('itemid') },
            success: function (msg, textStatus) {
                if (msg == '1') {
                    $n.val(v);
                    cart.refAmount();
                } else {
                    $.Alert('网络繁忙，请稍后再试...', 160);
                }
            },
            error: function (textStatus) {
                $.Alert('网络繁忙，请稍后再试...', 160);
            }
        });
    },
    //增加
    addNum: function (obj) {
        var $n = $(obj).parent().find('input');
        var v = parseInt($n.val());
        var stock = parseInt($n.attr('maxnum'));
        v = !isNaN(v) ? v + 1 : 1;
        v = v > 0 ? v : 1;
        if (v > stock) {
            $.Alert('库存不足');
            return;
        }

        $.ajax({
            type: 'POST',
            url: '/ajax/cart.ashx',
            dataType: 'text',
            data: { "act": "modify", "qty": v, "itemid": $n.attr('itemid') },
            success: function (msg, textStatus) {
                if (msg == '1') {
                    $n.val(v);
                    cart.refAmount();
                } else {
                    $.Alert('网络繁忙，请稍后再试...', 160);
                }
            },
            error: function (textStatus) {
                $.Alert('网络繁忙，请稍后再试...', 160);
            }
        });
    },
    //删除购物车商品
    delItem: function (itemid, comid) {
        var doFun = function () {
            $.ajax({
                type: 'POST',
                url: '/ajax/cart.ashx',
                dataType: 'text',
                data: { "act": "delitem", "itemid": itemid },
                success: function (msg, textStatus) {
                    if (msg == '1') {
                        $('#li_' + itemid).remove();
                        if ($('#itemContainer ul li').length > 0)
                            cart.refAmount(); //刷新统计
                        else {
                            cart.showNoRecord();
                        }
                    } else {
                        $.Alert('网络繁忙，请稍后再试...', 160);
                    }
                },
                error: function (textStatus) {
                    $.Alert('网络繁忙，请稍后再试...', 160);
                }
            });
        }
        $.Confirm.show({ content: '确定删除吗?', sure: doFun });
    },
    //刷新统计
    refAmount: function () {
        var c = 0;
        var num = 0;
        $('#itemContainer ul li input[tag="buycount"]').each(function (i, n) {
            var $n = $(n);
            c += parseInt($n.val()) * parseFloat($n.attr('price'));
            num += parseInt($n.val());
        });
        $('#cartNum').text(num);
        $('#cartAmount').text(c.toFixed(2));
    },
    //加载
    load: function () {
        $(function () {
            cart.showCart();
            cart.refAmount();
            //结算
            $('#btnEnter').click(function () {
                if ($.CheckSubmit.submit(true)) {
                    if (cart.jsons.length > 0 && $('#itemContainer ul li').length > 0) {
                        window.location = '/WxPay/Confirm.aspx?comid=' + cart.comid; //确认订单
                    }
                }
            });
        });
    }
}